package com.example.daxiguaforharmony.slice;

import com.example.daxiguaforharmony.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
//import ohos.agp.components.webengine.WebView;
import ohos.agp.components.webengine.WebView;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;

/**
 * Full screen ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        // hide status bar
        this.getWindow().addFlags(WindowManager.LayoutConfig.MARK_ALLOW_EXTEND_LAYOUT);
        this.getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);

        super.setUIContent(ResourceTable.Layout_port_main);

        int score = (int)intent.getIntParam("score", 0);

        Text txt = (Text) findComponentById(ResourceTable.Id_txt);
        txt.setText("score: " + score);

        Button btnOk = (Button) findComponentById(ResourceTable.Id_btnOk);
        btnOk.setText("OK");

        btnOk.setClickedListener(l -> {
            int height = this.getScreenHeight(this);
            int width = this.getScreenWidth(this);
            //更改Button组件的内容
            btnOk.setText("我被点击了~height="+height+" width="+width);
        });


        WebView webView = (WebView) findComponentById(ResourceTable.Id_webInst);
        if (webView != null) {
            System.out.println("webview is ok===");
            webView.load("http://dev-7guq6sa6cd4c1900-1305021566.tcloudbaseapp.com/fb3dv6/");

        } else {
            System.out.println("webview is null===");
            btnOk.setText("OK1");
        }
    }

    public int getScreenHeight(Context context){
        DisplayManager displayManager = DisplayManager.getInstance();
        Display display = displayManager.getDefaultDisplay(context).get();
        if(display == null)
            return -1;
        Point outPoint = new Point();
        display.getRealSize(outPoint);
        return outPoint.getPointYToInt();
    }

    public int getScreenWidth(Context context){
        DisplayManager displayManager = DisplayManager.getInstance();
        Display display = displayManager.getDefaultDisplay(context).get();
        if(display == null)
            return -1;
        Point outPoint = new Point();
        display.getRealSize(outPoint);
        return outPoint.getPointXToInt();
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
